package com.gxa.p2p.service.impl;

import com.gxa.p2p.mapper.WithdrawalMapper;
import com.gxa.p2p.pojo.*;
import com.gxa.p2p.service.WithdrawalService;
import com.gxa.p2p.util.MD5Creater;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: cxy
 * Date: 2020/08/03 12:14
 * Description:
 * Version: V1.0
 */
@Service("withdrawalService")
@Transactional(readOnly = true, isolation = Isolation.DEFAULT, propagation = Propagation.SUPPORTS)
public class WithdrawalServiceImpl implements WithdrawalService {


    @Resource
    private WithdrawalMapper withdrawalMapper;


    /**
     *  统计总数和带条件的总数统计
     * @param map 统计总数的条件
     * @return 返回总数
     */
    @Override
    public Integer count(Map<String, Object> map) {
        Integer count = withdrawalMapper.count(map);
        return count;
    }
    /**
     * 分页和模糊查询的方法
     * @param map 分页和模糊查询的条件
     * @return 返回查询到的对象集合
     */
    @Override
    public List<Withdrawal> selectLimitLike(Map<String, Object> map) {
        // 对传入的page和limit进行处理
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer)map.get("limit");
        map.put("page",(page-1)*limit);
        List<Withdrawal> selectList = withdrawalMapper.selectLimitLike(map);
        return selectList;
    }

    /**
     *  根据用户电话号码查询记录
     * @param userInfo 传入用户对象
     * @return 查询到的集合
     */
    @Override
    public List<Withdrawal> selectByPhoneNumber(UserInfo userInfo) {
        List<Withdrawal> withdrawalList = withdrawalMapper.selectByPhoneNumber(userInfo);
        return withdrawalList;
    }

    /**
     * 修改方法
     * @param withdrawal 传入的修改的对象
     * @return 返回影响的行数
     */
    @Override
    @Transactional(readOnly = false, isolation = Isolation.READ_COMMITTED, propagation = Propagation.REQUIRED, rollbackFor = {Throwable.class})
    public Integer update(Withdrawal withdrawal) {
        Integer updateCount = withdrawalMapper.updateByPrimaryKeySelective(withdrawal);



        return updateCount;
    }

    @Override
    public List<Withdrawal> selectAll() {

        return withdrawalMapper.selectAll();
    }

    /**
     * 提现订单处理方法
     */
    @Override
    public void doWithdraw(Withdrawal withdrawal) {
        Withdrawal myWithdrawal = withdrawalMapper.selectByPrimaryKey(withdrawal.getId());
        AccountInfo accountInfo = withdrawalMapper.getAccountInfo(myWithdrawal.getUserId());
        accountInfo.setFrozenAmount(accountInfo.getFrozenAmount().subtract(myWithdrawal.getWithdrawalAmount()));
        if (withdrawal.getStatus()==22) {
            accountInfo.setUsableAmount(accountInfo.getUsableAmount().add(myWithdrawal.getWithdrawalAmount()));
            withdrawalMapper.updateAccountInfo(accountInfo);
        } else if (withdrawal.getStatus()==21) {
            withdrawalMapper.updateAccountInfo(accountInfo);
        }
    }

    /**
     * 创建提现订单的方法,请求后就会调用该方法
     *
     * @param withdrawal 提现订单对象
     */
    @Override
    public void withdrawRequest(Withdrawal withdrawal) {
        withdrawal.setStatus(20);
        withdrawal.setServiceCharge(withdrawal.getWithdrawalAmount().multiply(new BigDecimal(0.001)));
        withdrawal.setEstimatedAmount(withdrawal.getWithdrawalAmount().subtract(withdrawal.getWithdrawalAmount().multiply(new BigDecimal(0.001))));
        withdrawalMapper.insertSelective(withdrawal);
    }

    @Override
    public String verify(Withdrawal withdrawal) {

        AccountInfo accountInfo = withdrawalMapper.getAccountInfo(withdrawal.getUserId());
        if (MD5Creater.encode(withdrawal.getWitPassword()).equals(accountInfo.getTradePassword())) {
            if (accountInfo.getUsableAmount().compareTo(withdrawal.getWithdrawalAmount()) != -1 && withdrawal.getWithdrawalAmount().compareTo(BigDecimal.ZERO) == 1) {
                accountInfo.setUsableAmount(accountInfo.getUsableAmount().subtract(withdrawal.getWithdrawalAmount()));
                accountInfo.setFrozenAmount(accountInfo.getFrozenAmount().add(withdrawal.getWithdrawalAmount()));
                withdrawalMapper.updateAccountInfo(accountInfo);
                return "成功";
            } else if (withdrawal.getWithdrawalAmount().compareTo(BigDecimal.ZERO) != 1) {
                return "提现金额异常";
            } else {
                return "账户可用余额不足";
            }
        } else {
            return "密码";
        }

    }
    @Override
    public List<Withdrawal> selectLikeLimit(Map<String, Object> map) {
        // 对传入的page和limit进行处理
        Integer page = (Integer) map.get("page");
        Integer limit = (Integer)map.get("limit");
        map.put("page",(page-1)*limit);
        if(map.get("id") != null){
            map.put("id","%"+map.get("id")+"%");
        } if(map.get("userId") != null){
            map.put("userId","%"+map.get("userId")+"%");
        } if (map.get("realName") != null) {
            map.put("realName","%"+map.get("realName")+"%");
        }
         if (map.get("serviceCharge") != null) {
            map.put("serviceCharge","%"+map.get("serviceCharge")+"%");
        }
         if (map.get("status") != null) {
            map.put("status","%"+map.get("status")+"%");
        }
         if (map.get("phoneNumber") != null) {
            map.put("phoneNumber","%"+map.get("phoneNumber")+"%");
        }
         if (map.get("bankName") != null) {
            map.put("bankName","%"+map.get("bankName")+"%");
        }
         if (map.get("withdrawalAmount") != null) {
            map.put("withdrawalAmount","%"+map.get("withdrawalAmount")+"%");
        }
        List<Withdrawal> selectList = withdrawalMapper.selectLikeLimit(map);
        return selectList;
    }

}
